package com.joycessh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.joycessh.domain.Result;
import com.joycessh.domain.ResultCode;
import com.joycessh.domain.crawl.SeedList;
import com.joycessh.service.SeedListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Joycessh
 * @Description
 * @Date 5/9/19 5:10 PM
 */
@Slf4j
@Api(description = "SeedList增删改查接口")
@RestController
@RequestMapping("/api/v1/seed/list")
public class SeedListController {

    @Autowired
    private SeedListService seedListService;

    @ApiOperation(value = "添加", notes = "根据前台输入的信息添加SeedList到数据库")
    @PostMapping("")
    public Result addSeedList(@RequestBody SeedList seedList) {
        if (seedListService.save(seedList)) {
            log.info("添加SeedList的自增主键为：" + seedList.getId());
            return Result.success().addData("SeedList", seedList);
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "删除SeedList", notes = "根据seedListId删除数据")
    @DeleteMapping("/{id}")
    public Result removeSeedList(@PathVariable Integer id) {
        if (seedListService.delete(id)) {
            return Result.success();
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "更新SeedList")
    @PutMapping(value = "")
    public Result updateSeedList(@RequestBody SeedList seedList) {
        if (seedListService.update(seedList)) {
            return Result.success();
        }
        return Result.failure(ResultCode.SERVICE_ERROR);
    }

    @ApiOperation(value = "获取所有的SeedList分页数据")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findAllSeedList(@PathVariable Integer currentPage,
                                  @PathVariable Integer pageSize) {
        log.info(currentPage.toString() );
        log.info(pageSize.toString());
        PageHelper.startPage(currentPage, pageSize);
        List<SeedList> lists = seedListService.findAll();
        PageInfo<SeedList> pageInfo = new PageInfo<>(lists);
        return Result.success().addData("seedLists", pageInfo);
    }

    @ApiOperation(value = "获取SeedList", notes = "根据id获取SeedList")
    @GetMapping("/{id}")
    public Result getSeedList(@PathVariable Integer id) {
        SeedList seedList = seedListService.getSeedList(id);
        return Result.success().addData("seedList", seedList);
    }
}
